package com.xxb.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xxb.constant.ResponseCode;
import com.xxb.model.entity.SysMenu;
import com.xxb.model.service.ISysMenuService;
import com.xxb.utils.DtoUtil;
import com.xxb.web.dto.ResultJson;
import com.xxb.web.dto.SysMenuDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 陈亮
 * @since 2018-07-07
 */
@Api(tags = "sys_menu - Controller")
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
    private ISysMenuService isys_menuService;

    @ApiOperation(value = "添加 - sys_menu")
    @PostMapping(value = "insert")
    public ResultJson insert(@RequestBody SysMenuDto model) {
        SysMenu entity = DtoUtil.convertObject(model, SysMenu.class);
        boolean flag = isys_menuService.insert(entity);
        if (flag) {
            return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(null);
        } else {
            return ResultJson.getInstance().setCode(ResponseCode.请求失败).setData(null);
        }
    }

    @ApiOperation(value = "修改 - sys_menu")
    @PutMapping(value = "up/{id}")
    public ResultJson update(@RequestBody SysMenuDto model) {
        SysMenu entity = DtoUtil.convertObject(model, SysMenu.class);
        boolean flag = isys_menuService.update(entity, null);
        if (flag) {
            return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(null);
        } else {
            return ResultJson.getInstance().setCode(ResponseCode.请求失败).setData(null);
        }
    }

    @ApiOperation(value = "删除 - sys_menu")
    @DeleteMapping(value = "delete/{id}")
    public ResultJson delete(@PathVariable Integer id) {
        boolean flag = isys_menuService.deleteById(id);
        if (flag) {
            return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(null);
        } else {
            return ResultJson.getInstance().setCode(ResponseCode.请求失败).setData(null);
        }
    }

    @ApiOperation(value = "查询所有 - sys_menu")
    @GetMapping(value = "query")
    public ResultJson query() {
        Page<SysMenu> page = new Page<SysMenu>();
        Page<SysMenu> list = page.setRecords(isys_menuService.selectList(null));
        return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(list);
    }


    @ApiOperation(value = "条件查询 - sys_menu")
    @PostMapping(value = "pagebycondtion/{current}/{size}/{col}/{val}")
    public ResultJson<Page<SysMenuDto>> pageByCondtion(@PathVariable Integer current, @PathVariable Integer size, @PathVariable String col, @PathVariable String val) {
        Page<SysMenu> page = new Page<SysMenu>(current, size);
        Wrapper<SysMenu> menuWrapper = new EntityWrapper<SysMenu>().like(col, val);
        List<SysMenu> list = isys_menuService.pageByCondtion(page, menuWrapper);
        Page<SysMenu> menuPage = page.setRecords(list);
        return ResultJson.getInstance().setCode(ResponseCode.请求成功).setData(menuPage);
    }
}

